package org.springcloud.plugin.framework.configuration;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import org.springcloud.plugin.framework.decorator.ZoneAvoidanceRuleDecorator;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author yanggld
 * @Date 2019/12/17-17:57
 * @Description
 * 基于规则的负载均衡调用时的调用规则
 */
@Configuration
@AutoConfigureBefore(RibbonClientConfiguration.class)
public class PluginLoadBalanceConfiguration {

	@Bean
	@ConditionalOnMissingBean
	public IRule ribbonRule(IClientConfig config) {
		//TODO 对该服务已经自定义了规则
//		if (this.propertiesFactory.isSet(IRule.class, serviceId)) {
//			return this.propertiesFactory.get(IRule.class, config, serviceId);
//		}
		ZoneAvoidanceRuleDecorator rule = new ZoneAvoidanceRuleDecorator();
		rule.initWithNiwsConfig(config);
		return rule;
	}
}
